<?php echo $header; ?>
<div id="content">
    <div class="breadcrumb">
        <?php foreach ($breadcrumbs as $breadcrumb) { ?>
        <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
        <?php } ?>
    </div>
    <?php if ($error_warning) { ?>
    <div class="warning"><?php echo $error_warning; ?></div>
    <?php } ?>
    <div class="box">
        <div class="heading">
            <h1><img src="view/image/shipping.png" alt="" /> <?php echo $heading_title; ?></h1>
            <div class="buttons"><a onclick="$('#form :input').removeAttr('disabled');
                    $('#form').submit();" class="button"><?php echo $button_save; ?></a><a href="<?php echo $cancel; ?>" class="button"><?php echo $button_cancel; ?></a></div>
        </div>
        <div class="content">
            <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
                <table class="form">
                    <tr>
                        <td><label for="speedy_server_address"><?php echo $entry_server_address; ?></label></td>
                        <td><input type="text" id="speedy_server_address" name="speedy_server_address" value="<?php echo $speedy_server_address; ?>" /></td>
                    </tr>
                    <tr>
                        <td><span class="required">*</span> <label for="speedy_username"><?php echo $entry_username; ?></label></td>
                        <td><input type="text" id="speedy_username" name="speedy_username" value="<?php echo $speedy_username; ?>" />
                            <?php if ($error_username) { ?>
                            <span class="error"><?php echo $error_username; ?></span>
                            <?php } ?></td>
                    </tr>
                    <tr>
                        <td><span class="required">*</span> <label for="speedy_password"><?php echo $entry_password; ?></label></td>
                        <td><input type="password" id="speedy_password" name="speedy_password" value="<?php echo $speedy_password; ?>" />
                            <?php if ($error_password) { ?>
                            <span class="error"><?php echo $error_password; ?></span>
                            <?php } ?></td>
                    </tr>
                    <tr>
                        <td></label></td>
                        <td><a  class="button" id="speedy_check_credentials" onclick="checkSpeedyCredentials();
                                return false;"><?php echo $button_testcredentials; ?></a></td>

                    </tr>
                    <tr>
                        <td><span class="required">*</span> <label for="speedy_name"><?php echo $entry_name; ?></label></td>
                        <td><input type="text" id="speedy_name" name="speedy_name" value="<?php echo $speedy_name; ?>" />
                            <?php if ($error_name) { ?>
                            <span class="error"><?php echo $error_name; ?></span>
                            <?php } ?></td>
                    </tr>
                    <tr>
                        <td><span class="required">*</span> <label for="speedy_telephone"><?php echo $entry_telephone; ?></label></td>
                        <td><input type="text" id="speedy_telephone" name="speedy_telephone" value="<?php echo $speedy_telephone; ?>" />
                            <?php if ($error_telephone) { ?>
                            <span class="error"><?php echo $error_telephone; ?></span>
                            <?php } ?></td>
                    </tr>
                    <tr>
                        <td><label for="speedy_workingtime_end_hour"><?php echo $entry_workingtime_end; ?></label></td>
                        <td><select id="speedy_workingtime_end_hour" name="speedy_workingtime_end_hour">
                                <?php for ($i = 0; $i < 24; $i++) { ?>
                                <?php $hour = str_pad($i, 2, '0', STR_PAD_LEFT); ?>
                                <?php if ($hour == $speedy_workingtime_end_hour) { ?>
                                <option value="<?php echo $hour; ?>" selected="selected"><?php echo $hour; ?></option>
                                <?php } else { ?>
                                <option value="<?php echo $hour; ?>"><?php echo $hour; ?></option>
                                <?php } ?>
                                <?php } ?>
                            </select>
                            <select id="speedy_workingtime_end_min" name="speedy_workingtime_end_min">
                                <?php for ($i = 0; $i < 60; $i++) { ?>
                                <?php $hour = str_pad($i, 2, '0', STR_PAD_LEFT); ?>
                                <?php if ($hour == $speedy_workingtime_end_min) { ?>
                                <option value="<?php echo $hour; ?>" selected="selected"><?php echo $hour; ?></option>
                                <?php } else { ?>
                                <option value="<?php echo $hour; ?>"><?php echo $hour; ?></option>
                                <?php } ?>
                                <?php } ?>
                            </select></td>
                    </tr>
                    <tr>
                        <td><span class="required">*</span> <?php echo $entry_allowed_methods; ?></td>
                        <td><div class="scrollbox">
                                <?php $class = 'odd'; ?>
                                <?php foreach ($services as $service_id => $service) { ?>
                                <?php $class = ($class == 'even' ? 'odd' : 'even'); ?>
                                <div class="<?php echo $class; ?>">
                                    <?php if (in_array($service_id, $speedy_allowed_methods)) { ?>
                                    <input type="checkbox" name="speedy_allowed_methods[]" value="<?php echo $service_id; ?>" checked="checked" />
                                    <?php echo $service; ?>
                                    <?php } else { ?>
                                    <input type="checkbox" name="speedy_allowed_methods[]" value="<?php echo $service_id; ?>" />
                                    <?php echo $service; ?>
                                    <?php } ?>
                                </div>
                                <?php } ?>
                            </div>
                            <a onclick="$(this).parent().find(':checkbox').attr('checked', true);"><?php echo $text_select_all; ?></a> / <a onclick="$(this).parent().find(':checkbox').attr('checked', false);"><?php echo $text_unselect_all; ?></a>
                            <?php if ($error_allowed_methods) { ?>
                            <span class="error"><?php echo $error_allowed_methods; ?></span>
                            <?php } ?></td>
                    </tr>
                    <tr>
                        <td><label for="speedy_pricing"><?php echo $entry_pricing; ?></label></td>
                        <td><select id="speedy_pricing" name="speedy_pricing" onchange="changePricing();">
                                <?php foreach ($pricings as $pricing_id => $pricing) { ?>
                                <?php if ($pricing_id == $speedy_pricing) { ?>
                                <option value="<?php echo $pricing_id; ?>" selected="selected"><?php echo $pricing; ?></option>
                                <?php } else { ?>
                                <option value="<?php echo $pricing_id; ?>"><?php echo $pricing; ?></option>
                                <?php } ?>
                                <?php } ?>
                            </select></td>
                    </tr>
                    <tr <?php if ($speedy_pricing != 'fixed' && $speedy_pricing != 'calculator_fixed') { ?> style="display: none;"<?php } ?>>
                        <td><label for="speedy_fixed_price"><?php echo $entry_fixed_price; ?></label></td>
                        <td><input type="text" id="speedy_fixed_price" name="speedy_fixed_price" value="<?php echo $speedy_fixed_price; ?>" /></td>
                    </tr>
                    <tr <?php if ($speedy_pricing != 'free') { ?> style="display: none;"<?php } ?>>
                        <td><label for="speedy_free_shipping_total"><?php echo $entry_free_shipping_total ?></label></td>
                        <td><input type="text" id="speedy_free_shipping_total" name="speedy_free_shipping_total" value="<?php echo $speedy_free_shipping_total; ?>" /></td>
                    </tr>
                    <tr <?php if ($speedy_pricing != 'free') { ?> style="display: none;"<?php } ?>>
                        <td><label for="speedy_free_method_city"><?php echo $entry_free_method_city; ?></label></td>
                        <td><select id="speedy_free_method_city" name="speedy_free_method_city">
                                <?php foreach ($services as $service_id => $service) { ?>
                                <?php if ($service_id == $speedy_free_method_city) { ?>
                                <option value="<?php echo $service_id; ?>" selected="selected"><?php echo $service; ?></option>
                                <?php } else { ?>
                                <option value="<?php echo $service_id; ?>"><?php echo $service; ?></option>
                                <?php } ?>
                                <?php } ?>
                            </select></td>
                    </tr>
                    <tr <?php if ($speedy_pricing != 'free') { ?> style="display: none;"<?php } ?>>
                        <td><label for="speedy_free_method_intercity"><?php echo $entry_free_method_intercity; ?></label></td>
                        <td><select id="speedy_free_method_intercity" name="speedy_free_method_intercity">
                                <?php foreach ($services as $service_id => $service) { ?>
                                <?php if ($service_id == $speedy_free_method_intercity) { ?>
                                <option value="<?php echo $service_id; ?>" selected="selected"><?php echo $service; ?></option>
                                <?php } else { ?>
                                <option value="<?php echo $service_id; ?>"><?php echo $service; ?></option>
                                <?php } ?>
                                <?php } ?>
                            </select></td>
                    </tr>
                    <tr>
                        <td><label for="speedy_back_documents"><?php echo $entry_back_documents; ?></label></td>
                        <td><select id="speedy_back_documents" name="speedy_back_documents">
                                <?php if ($speedy_back_documents) { ?>
                                <option value="1" selected="selected"><?php echo $text_yes; ?></option>
                                <option value="0"><?php echo $text_no; ?></option>
                                <?php } else { ?>
                                <option value="1"><?php echo $text_yes; ?></option>
                                <option value="0" selected="selected"><?php echo $text_no; ?></option>
                                <?php } ?>
                            </select></td>
                    </tr>
                    <tr>
                        <td><label for="speedy_back_receipt"><?php echo $entry_back_receipt; ?></label></td>
                        <td><select id="speedy_back_receipt" name="speedy_back_receipt">
                                <?php if ($speedy_back_receipt) { ?>
                                <option value="1" selected="selected"><?php echo $text_yes; ?></option>
                                <option value="0"><?php echo $text_no; ?></option>
                                <?php } else { ?>
                                <option value="1"><?php echo $text_yes; ?></option>
                                <option value="0" selected="selected"><?php echo $text_no; ?></option>
                                <?php } ?>
                            </select></td>
                    </tr>
                    <tr>
                        <td><span class="required">*</span> <label for="speedy_default_weight"><?php echo $entry_default_weight; ?></label></td>
                        <td><input type="text" id="speedy_default_weight" name="speedy_default_weight" value="<?php echo $speedy_default_weight; ?>" />
                            <?php if ($error_default_weight) { ?>
                            <span class="error"><?php echo $error_default_weight; ?></span>
                            <?php } ?></td>
                    </tr>
                    <tr>
                        <td><label for="speedy_packing"><?php echo $entry_packing; ?></label></td>
                        <td><input type="text" id="speedy_packing" name="speedy_packing" value="<?php echo $speedy_packing; ?>" /></td>
                    </tr>
                    <tr>
                        <td><label for="speedy_label_printer"><?php echo $entry_label_printer; ?></label></td>
                        <td><select id="speedy_label_printer" name="speedy_label_printer">
                                <?php if ($speedy_label_printer) { ?>
                                <option value="1" selected="selected"><?php echo $text_yes; ?></option>
                                <option value="0"><?php echo $text_no; ?></option>
                                <?php } else { ?>
                                <option value="1"><?php echo $text_yes; ?></option>
                                <option value="0" selected="selected"><?php echo $text_no; ?></option>
                                <?php } ?>
                            </select></td>
                    </tr>
                    <tr>
                        <td><label for="speedy_insurance"><?php echo $entry_insurance; ?></label></td>
                        <td><select id="speedy_insurance" name="speedy_insurance" onchange="$('#speedy_fragile').parent().parent().toggle();">
                                <?php if ($speedy_insurance) { ?>
                                <option value="1" selected="selected"><?php echo $text_yes; ?></option>
                                <option value="0"><?php echo $text_no; ?></option>
                                <?php } else { ?>
                                <option value="1"><?php echo $text_yes; ?></option>
                                <option value="0" selected="selected"><?php echo $text_no; ?></option>
                                <?php } ?>
                            </select></td>
                    </tr>
                    <tr <?php if (!$speedy_insurance) { ?> style="display: none;"<?php } ?>>
                        <td><label for="speedy_fragile"><?php echo $entry_fragile; ?></label></td>
                        <td><select id="speedy_fragile" name="speedy_fragile">
                                <?php if ($speedy_fragile) { ?>
                                <option value="1" selected="selected"><?php echo $text_yes; ?></option>
                                <option value="0"><?php echo $text_no; ?></option>
                                <?php } else { ?>
                                <option value="1"><?php echo $text_yes; ?></option>
                                <option value="0" selected="selected"><?php echo $text_no; ?></option>
                                <?php } ?>
                            </select></td>
                    </tr>
                    <tr>
                        <td><label for="speedy_from_office"><?php echo $entry_from_office; ?></label></td>
                        <td><select id="speedy_from_office" name="speedy_from_office" onchange="$('#speedy_office_id').parent().parent().toggle();">
                                <?php if ($speedy_from_office) { ?>
                                <option value="1" selected="selected"><?php echo $text_yes; ?></option>
                                <option value="0"><?php echo $text_no; ?></option>
                                <?php } else { ?>
                                <option value="1"><?php echo $text_yes; ?></option>
                                <option value="0" selected="selected"><?php echo $text_no; ?></option>
                                <?php } ?>
                            </select></td>
                    </tr>
                    <tr <?php if (!$speedy_from_office) { ?> style="display: none;"<?php } ?>>
                        <td><label for="speedy_office_id"><?php echo $entry_office; ?></label></td>
                        <td><select id="speedy_office_id" name="speedy_office_id" style="width: 350px;">
                                <?php foreach ($offices as $office) { ?>
                                <?php if ($office['id'] == $speedy_office_id) { ?>
                                <option value="<?php echo $office['id']; ?>" selected="selected"><?php echo $office['label']; ?></option>
                                <?php } else { ?>
                                <option value="<?php echo $office['id']; ?>"><?php echo $office['label']; ?></option>
                                <?php } ?>
                                <?php } ?>
                            </select></td>
                    </tr>
                    <tr>
                        <td><label for="speedy_documents"><?php echo $entry_documents; ?></label></td>
                        <td><select id="speedy_documents" name="speedy_documents">
                                <?php if ($speedy_documents) { ?>
                                <option value="1" selected="selected"><?php echo $text_yes; ?></option>
                                <option value="0"><?php echo $text_no; ?></option>
                                <?php } else { ?>
                                <option value="1"><?php echo $text_yes; ?></option>
                                <option value="0" selected="selected"><?php echo $text_no; ?></option>
                                <?php } ?>
                            </select></td>
                    </tr>
                    <tr>
                        <td><label for="speedy_fixed_time"><?php echo $entry_fixed_time; ?></label></td>
                        <td><select id="speedy_fixed_time" name="speedy_fixed_time">
                                <?php if ($speedy_fixed_time) { ?>
                                <option value="1" selected="selected"><?php echo $text_yes; ?></option>
                                <option value="0"><?php echo $text_no; ?></option>
                                <?php } else { ?>
                                <option value="1"><?php echo $text_yes; ?></option>
                                <option value="0" selected="selected"><?php echo $text_no; ?></option>
                                <?php } ?>
                            </select></td>
                    </tr>
                    <tr>
                        <td><label for="speedy_taking_date"><?php echo $entry_taking_date; ?></label></td>
                        <td><input type="text" id="speedy_taking_date" name="speedy_taking_date" value="<?php echo $speedy_taking_date; ?>" /></td>
                    </tr>
                    <tr>
                        <td><label for="speedy_currency"><?php echo $entry_currency; ?></label></td>
                        <td><select id="speedy_currency" name="speedy_currency">
                                <?php foreach ($currencies as $currency) { ?>
                                <?php if ($currency['code'] == $speedy_currency) { ?>
                                <option value="<?php echo $currency['code']; ?>" selected="selected"><?php echo $currency['title']; ?></option>
                                <?php } else { ?>
                                <option value="<?php echo $currency['code']; ?>"><?php echo $currency['title']; ?></option>
                                <?php } ?>
                                <?php } ?>
                            </select></td>
                    </tr>
                    <!--<tr>
                      <td><label for="speedy_weight_class_id"><?php echo $entry_weight_class; ?></label></td>
                      <td><select id="speedy_weight_class_id" name="speedy_weight_class_id">
                          <?php foreach ($weight_classes as $weight_class) { ?>
                          <?php if ($weight_class['weight_class_id'] == $speedy_weight_class_id) { ?>
                          <option value="<?php echo $weight_class['weight_class_id']; ?>" selected="selected"><?php echo $weight_class['title']; ?></option>
                          <?php } else { ?>
                          <option value="<?php echo $weight_class['weight_class_id']; ?>"><?php echo $weight_class['title']; ?></option>
                          <?php } ?>
                          <?php } ?>
                        </select></td>
                    </tr>-->
                    <tr>
                        <td><label for="speedy_order_status_id"><?php echo $entry_order_status; ?></label></td>
                        <td><select id="speedy_order_status_id" name="speedy_order_status_id">
                                <?php foreach ($order_statuses as $order_status) { ?>
                                <?php if(!$speedy_order_status_id) { ?>
                                <?php if($order_status['order_status_id'] == 3) { ?>

                                <option value="<?php echo $order_status['order_status_id']; ?>" selected="selected"><?php echo $order_status['name']; ?></option>
                                <?php } ?>
                                <?php } ?>


                                <?php if ($order_status['order_status_id'] == $speedy_order_status_id) { ?>
                                <option value="<?php echo $order_status['order_status_id']; ?>" selected="selected"><?php echo $order_status['name']; ?></option>
                                <?php } else { ?>
                                <option value="<?php echo $order_status['order_status_id']; ?>"><?php echo $order_status['name']; ?></option>
                                <?php } ?>
                                <?php } ?>
                            </select></td>
                    </tr>
                    <tr>
                        <td><label for="speedy_geo_zone_id"><?php echo $entry_geo_zone; ?></label></td>
                        <td><select id="speedy_geo_zone_id" name="speedy_geo_zone_id">
                                <option value="0"><?php echo $text_all_zones; ?></option>
                                <?php foreach ($geo_zones as $geo_zone) { ?>
                                <?php if ($geo_zone['geo_zone_id'] == $speedy_geo_zone_id) { ?>
                                <option value="<?php echo $geo_zone['geo_zone_id']; ?>" selected="selected"><?php echo $geo_zone['name']; ?></option>
                                <?php } else { ?>
                                <option value="<?php echo $geo_zone['geo_zone_id']; ?>"><?php echo $geo_zone['name']; ?></option>
                                <?php } ?>
                                <?php } ?>
                            </select></td>
                    </tr>
                    <tr>
                        <td><label for="speedy_status"><?php echo $entry_status; ?></label></td>
                        <td><select id="speedy_status" name="speedy_status">
                                <?php if ($speedy_status) { ?>
                                <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
                                <option value="0"><?php echo $text_disabled; ?></option>
                                <?php } else { ?>
                                <option value="1"><?php echo $text_enabled; ?></option>
                                <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
                                <?php } ?>
                            </select></td>
                    </tr>
                    <tr>
                        <td><label for="speedy_sort_order"><?php echo $entry_sort_order; ?></label></td>
                        <td><input type="text" id="speedy_sort_order" name="speedy_sort_order" value="<?php echo $speedy_sort_order; ?>" size="1" /></td>
                    </tr>
                </table>
            </form>
        </div>
    </div>
</div>
<script type="text/javascript"><!--
    function checkSpeedyCredentials() {


    var speedyUsername = document.getElementById("speedy_username").value;
            var speedyPassword = document.getElementById("speedy_password").value;
            if (!speedyUsername) {

    alert("<?php echo $error_username_credentials; ?>")
    } else if (!speedyPassword) {
    alert("<?php echo $error_password_credentials; ?>")
    }
    else {


    $.ajax({
    url: 'index.php?route=shipping/speedy/checkcredentials&token=<?php echo $token; ?>',
            type: 'POST',
            data: {username: speedyUsername, password: speedyPassword},
            dataType: 'json',
            success: function(data) {
            if (data.ok) {

            alert(data.message);
            } else if (data.error){
            alert(data.message);
            }
            },
            error: function(xhr, ajaxOptions, thrownError) {
            //alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
    });
       }

    }

    
            function changePricing() {
            pricing = $('#speedy_pricing').val();
                    if (pricing == 'free') {
            $('#speedy_fixed_price').parent().parent().hide();
                    $('#speedy_free_shipping_total').parent().parent().show();
                    $('#speedy_free_method_city').parent().parent().show();
                    $('#speedy_free_method_intercity').parent().parent().show();
            } else if (pricing == 'fixed' || pricing == 'calculator_fixed') {
            $('#speedy_fixed_price').parent().parent().show();
                    $('#speedy_free_shipping_total').parent().parent().hide();
                    $('#speedy_free_method_city').parent().parent().hide();
                    $('#speedy_free_method_intercity').parent().parent().hide();
            } else {
            $('#speedy_fixed_price').parent().parent().hide();
                    $('#speedy_free_shipping_total').parent().parent().hide();
                    $('#speedy_free_method_city').parent().parent().hide();
                    $('#speedy_free_method_intercity').parent().parent().hide();
            }
            }
//--></script>
<?php echo $footer; ?>